package com.atguigu3;

import com.beans.Customers;
import com.beans.Order;
import com.utils.JDBCUtils;
import org.junit.Test;

import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;

/**
 * @auther user
 * @date 2021/12/6 - 15:19
 */
public class PreparedStatementQueryTest {
    /**
     *  针对于不同表的通用查询操作，返回表中的一条记录
     * @param clazz
     * @param sql
     * @param args
     * @param <T>
     * @return
     */
    public <T> T getInstance(Class<T> clazz,String sql,Object ...args) {
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //1 获取连接
            connection = JDBCUtils.getConnection();
            //2 预编译sql,返回PreparStatement实例
            preparedStatement = connection.prepareStatement(sql);
            //3 填充占位符
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i+1,args[i]);
            }
            //4 执行，返回ResultSet结果集
            resultSet = preparedStatement.executeQuery();
            //5 处理结果集
            //获取元数据
            ResultSetMetaData metaData = resultSet.getMetaData();
            //获取总列数
            int columnCount = metaData.getColumnCount();
            if (resultSet.next()) {
                T t = clazz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    //获取字段别名
                    String columnLabel = metaData.getColumnLabel(i + 1);
                    //获取字段值
                    Object columnValue = resultSet.getObject(i + 1);
                    //通过反射，给t对象的columnLabel属性赋值columValue
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t,columnValue);
                }
                return t;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //关闭资源
            JDBCUtils.closeResource(connection,preparedStatement,resultSet);
        }
        return null;
    }
    @Test
    public void testGetInstance(){
        String sql = "select order_id as orderId,order_name as orderName,order_date as orderDate from `order` where order_id = ?";
        Order order = getInstance(Order.class, sql, 1);
        System.out.println(order);

        String sql1 = "select id,name,email,birth from customers where id = ?";
        Customers customers = getInstance(Customers.class, sql1, 3);
        System.out.println(customers);
    }
    //针对于不同表的通用查询操作，返回list
    public <T> List<T> getForList(Class<T> clazz,String sql,Object ...args){
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        try {
            //1 获取连接
            connection = JDBCUtils.getConnection();
            //2 预编译sql,返回perparedStatment实例
            preparedStatement = connection.prepareStatement(sql);
            //3 填充占位符
            for (int i = 0; i < args.length; i++) {
                preparedStatement.setObject(i+1,args[i]);
            }
            //4 执行，返回ResultSet结果集
            resultSet = preparedStatement.executeQuery();
            //5 处理结果集
            //5.1 获取元数据
            ResultSetMetaData metaData = resultSet.getMetaData();
            //5.2 获取总列数
            int columnCount = metaData.getColumnCount();
            //5.3 创建结果集
            ArrayList<T> arrayList = new ArrayList<T>();
            while (resultSet.next()){
                T t = clazz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    //5.4 获取字段别名
                    String columnLabel = metaData.getColumnLabel(i + 1);
                    //5.5 获取字段值
                    Object columnValue = resultSet.getObject(i + 1);
                    //5.6 通过反射，将columnValue存储到t对象的columnLavel属性中
                    Field field = clazz.getDeclaredField(columnLabel);
                    field.setAccessible(true);
                    field.set(t,columnValue);
                }
                arrayList.add(t);
            }
            return arrayList;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //关闭资源
            JDBCUtils.closeResource(connection,preparedStatement,resultSet);
        }
        return null;
    }

    @Test
    public void testGetForList(){
        String sql = "select id,name,email,birth from customers";
        List<Customers> list = getForList(Customers.class, sql);
        list.forEach(System.out::println);
    }
}
